package my.soft.aiteacher.teacher.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import my.soft.aiteacher.exception.BusinessException;
import my.soft.aiteacher.teacher.mapStruct.LessonPlansMapStruct;
import my.soft.aiteacher.teacher.mapper.LessonPlansMapper;
import my.soft.aiteacher.teacher.pojo.TbLessonPlansDo;
import my.soft.aiteacher.teacher.pojo.TbLessonPlansVo;
import my.soft.aiteacher.teacher.service.ILessonPlansService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class LessonPlansImpl extends ServiceImpl<LessonPlansMapper, TbLessonPlansDo> implements ILessonPlansService {
    @Resource
    private LessonPlansMapper lessonPlansMapper;
    @Resource
    private LessonPlansMapStruct lessonPlansMapStruct;
    @Override
    public void submitLessonPlans(TbLessonPlansVo lessonPlans) {
        TbLessonPlansDo tbLessonPlansDo = lessonPlansMapper.selectOne(new QueryWrapper<TbLessonPlansDo>().eq("teacher_id", lessonPlans.getTeacherId()).eq("file_id", lessonPlans.getFileId()));
        if(Objects.nonNull(tbLessonPlansDo)){
            lessonPlansMapper.delete(new QueryWrapper<TbLessonPlansDo>().eq("teacher_id", lessonPlans.getTeacherId()).eq("file_id", lessonPlans.getFileId()));
        }
        TbLessonPlansDo aDo = lessonPlansMapStruct.toDo(lessonPlans);
        lessonPlansMapper.insert(aDo);
    }

    @Override
    public TbLessonPlansVo LessonPlansList(Long teacherId, Long fileId) {


        if(Objects.isNull(teacherId)){
            throw new BusinessException("为上传教师id，请联系管理员");
        }
        TbLessonPlansDo tbLessonPlansDo = lessonPlansMapper.selectOne(new QueryWrapper<TbLessonPlansDo>().eq("teacher_id", teacherId).eq("file_id", fileId));

        return lessonPlansMapStruct.toVo(tbLessonPlansDo) ;
    }
}
